import io
from os import times
import struct
import sys
import socket
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtCore import Qt, QThread
from Ui_MainWindow import Ui_MainWindow
import time
from PIL import Image
from PIL import ImageFile

class MyMainForm (QMainWindow, Ui_MainWindow):
        def __init__(self, parent=None):
                super().__init__(parent)
                self.setupUi(self)
                self.pushButton.clicked.connect(self.buttonClick)

        def buttonClick(self):
                time_start = time.time()
                ip = self.lineEdit_IP.text()
                port = self.lineEdit_Port.text()
                myIP = self.lineEdit_MyIp.text()
                myPort = self.lineEdit_MyPort.text()
                mySocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                mySocket.bind((str(myIP), int(myPort)))
                mySocket.connect((str(ip), int(port)))
                data1 = self.lineEdit.text()

                self.textBrowser.append("myIP:"+str(myIP))
                self.textBrowser.append("serverIP:"+str(ip))
                self.textBrowser.append("request:"+str(data1))

                mySocket.send(data1.encode())
                fileinfo_size = struct.calcsize('128sq')
                buf = mySocket.recv(fileinfo_size)
                self.textBrowser.append("start receive")

                if buf:
                        filename, filesize = struct.unpack('128sq', buf)
                        ImageFile.LOAD_TRUNCATED_IMAGES = True
                        recvd_size = 0
                        file_buf = ''.encode()
                        while not recvd_size == filesize:
                                if filesize - recvd_size > 1024:
                                        data = mySocket.recv(1024)
                                        recvd_size += len(data)
                                else:
                                        data = mySocket.recv(1024)
                                        recvd_size = filesize
                                file_buf = file_buf + data
                                
                                if (recvd_size /1024)%10 >= 9:
                                        ff=open('myout.jpg','wb')
                                        ff.write(file_buf)
                                        ff.close()
                                        self.label_3.setScaledContents(True)
                                        self.label_3.setPixmap(QPixmap('myout.jpg'))
                                        self.textBrowser.append("time:"+ str(round(time.time()-time_start, 4))+str('  size: ')+str(recvd_size))
                                        self.textBrowser.append("------------------------------")
                                        QApplication.processEvents()
                                
                        # img_io = io.BytesIO(file_buf)
                        # img = Image.open(img_io)
                        # pix = img.toqpixmap()
                        ff=open('myout.jpg','wb')
                        ff.write(file_buf)
                        ff.close()
                        self.label_3.setScaledContents(True)
                        self.label_3.setPixmap(QPixmap('myout.jpg'))
                        self.textBrowser.append("allTime:"+ str(round(time.time()-time_start, 4))+str('  success '))
                mySocket.close()

                 # data1 = mySocket.recv(1024)
                # self.textBrowser.append(data1.decode('utf-8'))


if __name__ == '__main__':
        app = QApplication(sys.argv)
        myWin = MyMainForm()
        myWin.show()
        sys.exit(app.exec_())
